var {
  clickContent,
  randomUtils,
  timeUtils,
  logUtils,
  clickUtils,
} = require ('../../utils/index');
var {Action} = require ('../../base/index');
const pointMap = require ('./pointMap');
var aWatch = new Action ('看视频', {});

aWatch.exec = function () {
  if (this.checkContinue ()) {
    this.watchVideo ();
    return;
  }
  if (randomUtils.possible (80, 100)) {
    var content = '万播放';
    if (clickContent (content)) {
      sleep (randomUtils.getRandom (2, 3, 1000));
      this.watchVideo ();
    }``
  } else {
    clickUtils.clickPointMap (pointMap.首页.福利);
    sleep (randomUtils.getRandom (5, 8, 1000));
    if (this.checkContinue ()) {
      this.watchVideo ();
      return;
    }
    sleep (randomUtils.getRandom (5, 8, 1000));
    var content = '去看剧';
    if (clickContent (content)) {
      sleep (randomUtils.getRandom (2, 3, 1000));
      this.watchVideo ();
      return;
    }
  }
};
aWatch.checkContinue = function () {
  var content = '继续观看';
  if (clickContent (content)) {
    sleep (randomUtils.getRandom (3, 5, 1000));
    return true;
  }
  return false;
};
aWatch.watchVideo = function () {
  var baseSpan = 2;
  var span = randomUtils.getRandom (baseSpan, baseSpan + 3, 60);
  logUtils.info ('本轮看视频' + span + '秒');
  var startTime = Date.now ();
  var count = 0;
  while (true) {
    sleep (randomUtils.getRandom (2, 4, 1000));
    count++;
    var hasExec = timeUtils.hasExec (startTime);
    if (count % 5 == 0) {
      logUtils.info ('已经看视频' + hasExec + '秒');
    }
    if (hasExec >= span) {
      if (count % 5 != 0) {
        logUtils.info ('已经看视频' + hasExec + '秒');
      }
      logUtils.info ('将结束看视频');
      break;
    }
  }
};

module.exports = aWatch;
